package com.iven.xie.fboot.tools.result;

import com.iven.xie.fboot.tools.util.StringUtil;
import lombok.Data;

import java.io.Serializable;

/**
 * 基础返回值
 *
 * @author iven.xie
 */
@Data
public class ResultModel<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 描述
     */
    private String msg;

    /**
     * 返回内容
     */
    private T data;

    public ResultModel() {

    }

    public ResultModel(ResultStatus status) {
        this.code = status.getCode();
        this.msg = status.getMsg();
    }

    public ResultModel(ResultStatus status, T data) {
        this.code = status.getCode();
        this.msg = status.getMsg();
        this.data = data;
    }

    public ResultModel(ResultStatus status, String msg, T data) {
        this.code = status.getCode();
        this.msg = msg;
        this.data = data;
    }

    public static ResultModel<String> defaultSuccess(String msg) {
        if (StringUtil.isNotBlank(msg)) {
            return new ResultModel<>(ResultStatus.SUCCESS, msg);
        } else {
            return new ResultModel<>(ResultStatus.SUCCESS, ResultStatus.SUCCESS.getMsg());
        }
    }

    public static ResultModel<String> defaultError(String msg) {
        if (StringUtil.isNotBlank(msg)) {
            return new ResultModel<>(ResultStatus.SYS_ERROR, msg);
        } else {
            return new ResultModel<>(ResultStatus.SYS_ERROR, ResultStatus.SYS_ERROR.getMsg());
        }
    }
}
